using System;
using System.Collections.Generic;
using System.Text;
using Utility;
using DAL;
using DTO;
namespace BUS
{
  public  class tbKhachHangBUS
    {
      public static void Insert(tbKhachHang khachhang)
        {
            tbKhachHangDAL khDAL = new tbKhachHangDAL();
            khDAL.Insert(khachhang);
        }
      public static void Update(tbKhachHang khachhang)
        {
            tbKhachHangDAL khDAl = new tbKhachHangDAL();
            khDAl.Update(khachhang);
        }
        public static void Delete(int iD)
        {
            tbKhachHangDAL khDAl = new tbKhachHangDAL();
            khDAl.Delete(iD);
        }
      public static tbKhachHangCollection Get(string where, int pageindex, int pagesize, string orderby, string orderdirection, out int total)
        {
            tbKhachHangDAL khDAL = new tbKhachHangDAL();
            return khDAL.Get(where, pageindex, pagesize, orderby, orderdirection, out total);
        }
      public static tbKhachHangCollection GetAll()
        {
            int total = 0;
            Criteria cri = new Criteria();
            tbKhachHangDAL khDAL = new tbKhachHangDAL();
            return khDAL.Get(cri.Criter, 0, 0, tbKhachHangColumns.makh .ToString(), OrderDirection.ASC.ToString(), out total);
        }
      public static tbKhachHang GetByID(int iD)
        {
            int total = 0;
            Criteria cri = new Criteria();
            cri.add(Condition.AND, tbKhachHangColumns.makh.ToString(), Condition.EQUAL, iD);
            tbKhachHangDAL khDAL = new tbKhachHangDAL();
            tbKhachHangCollection khCollection = khDAL.Get(cri.Criter, 0, 0, tbKhachHangColumns.makh.ToString(), OrderDirection.ASC.ToString(), out total);
            if (khCollection.Count > 0)
                return khCollection[0];
            return null;
        }
      public static tbKhachHangCollection GetLimited(int number)
        {
            int total = 0;
            Criteria cri = new Criteria();
            tbKhachHangDAL khDAL = new tbKhachHangDAL();
            return khDAL.Get(cri.Criter, 1, number, tbKhachHangColumns.makh.ToString(), OrderDirection.DESC.ToString(), out total);
        }
      public static tbKhachHangCollection GetPromotion(int number)
        {
            int total = 0;
            Criteria cri = new Criteria();
            tbKhachHangDAL khDAL = new tbKhachHangDAL();
            return khDAL.Get(cri.Criter, 1, number, tbKhachHangColumns.makh.ToString(), OrderDirection.DESC.ToString(), out total);
        }

        public static tbKhachHang GetByTenkh(string tenkh)
        {
            int total = 0;
            Criteria cri = new Criteria();
            cri.add(Condition.AND, tbKhachHangColumns.hotenkh.ToString(), Condition.EQUAL, tenkh);
            tbKhachHangDAL khDAL = new tbKhachHangDAL();
            tbKhachHangCollection khCollection = khDAL.Get(cri.Criter, 0, 0, tbKhachHangColumns.hotenkh.ToString(), OrderDirection.ASC.ToString(), out total);
            if (khCollection.Count > 0)
                return khCollection[0];
            return null;
        }

      public static tbKhachHang GetByTenDN(string Tendangnhap)
      {
          int t = 0;
          tbKhachHangDAL khmoi = new tbKhachHangDAL();
          tbKhachHangCollection  danhsach = new tbKhachHangCollection();
          danhsach = khmoi.Get(" Where email = " + "'" + Tendangnhap.ToString() + "'", 0, 0, tbKhachHangColumns.makh.ToString(), "ASC", out t);
          if (danhsach.Count == 1)

              return danhsach[0];
          return null;

      }

      public static tbKhachHang GetByUsername(string iD)
      {
          int total = 0;
          Criteria cri = new Criteria();
          cri.add(Condition.AND, tbKhachHangColumns.email.ToString(), Condition.LIKE, iD);
          tbKhachHangDAL khDAL = new tbKhachHangDAL();
          tbKhachHangCollection khCollection = khDAL.Get(cri.Criter, 0, 0, tbKhachHangColumns.makh.ToString(), OrderDirection.ASC.ToString(), out total);
          if (khCollection.Count > 0)
              return khCollection[0];
          return null;
      }
  }
}
